package com.ylx.web.u;

import com.ylx.pojo.User;
import com.ylx.service.UserService;
import com.ylx.service.UserServiceIpml;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {

    @Override
    protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          req.setCharacterEncoding("utf-8");
        String NAME = req.getParameter("NAME");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String tel = req.getParameter("tel");
        String role = req.getParameter("role");

        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setNAME(NAME);
        user.setTel(tel);
        user.setRole(role);


        UserService userService=new UserServiceIpml();
        User user1 = userService.selectByUsername(username);

        if(user1 == null){
              userService.add(user);
           req.setAttribute("login","注册成功请登录");
           req.getRequestDispatcher("login.jsp").forward(req,resp);

    }else {
//     数据库中有这个用户
            req.setAttribute("error","用户已存在");
            req.getRequestDispatcher("register.jsp").forward(req,resp);

       }
    }


    @Override
    protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     this.doGet(req,resp);
    }
}
